package com.example.common.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.File;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class ImgLoader {

    private static final boolean SKIP_MEMORY_CACHE = false;
    private static BlurTransformation sBlurTransformation;

    static {
        sBlurTransformation = new BlurTransformation(25);
    }

    public static void  loadImg(Context context, String img, ImageView imgView) {
        if (context == null || TextUtils.isEmpty(img)) {
            return;
        }
        Glide.with(context).asDrawable().load(img).skipMemoryCache(SKIP_MEMORY_CACHE).into(imgView);
    }

    public static void loadImg(Context context, Object img, ImageView imgView) {
        if (context == null) {
            return;
        }
        Glide.with(context).asDrawable().load(img).skipMemoryCache(SKIP_MEMORY_CACHE).into(imgView);
    }

    /**
     * 加载图片带有错误图
     * @param context
     * @param img
     * @param imgView
     * @param errorRes
     */
    public static void loadImgOrError(Context context, String img, ImageView imgView, int errorRes) {
        if (context == null) {
            return;
        }
        Glide.with(context).asDrawable().load(img).error(errorRes).skipMemoryCache(SKIP_MEMORY_CACHE).into(imgView);
    }

    /**
     * 加载圆角图片
     * @param context
     * @param img
     * @param imgView
     */
    public static void loadRoundImg(Context context, Object img, ImageView imgView) {
        if (context == null) {
            return;
        }
        Glide.with(context).asDrawable().load(img).apply(RequestOptions.circleCropTransform()).skipMemoryCache(SKIP_MEMORY_CACHE).into(imgView);
    }

    /**
     * 加载自定义圆角大小图片
     * @param context
     * @param img
     * @param radius 圆角角度
     * override:采样率
     */
    public static void loadRoundImg(Context context, Object img, int radius,ImageView imgView) {
        if (context == null) {
            return;
        }
        RoundedCorners roundedCorners = new RoundedCorners(radius);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(60, 60);
        Glide.with(context).asDrawable().load(img).apply(options).skipMemoryCache(SKIP_MEMORY_CACHE).into(imgView);
    }

    /**
     * 显示视频封面缩略图
     */
    public static void loadVideoThumb(Context context, String videoPath, ImageView imgView) {
        Glide.with(context).asDrawable().load(Uri.fromFile(new File(videoPath))).skipMemoryCache(SKIP_MEMORY_CACHE).into(imgView);
    }

    public static void loadDrawable(Context context, String imgUrl, final DrawableCallback callback) {
        Glide.with(context).asDrawable().load(imgUrl).skipMemoryCache(SKIP_MEMORY_CACHE).into(new CustomTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                if (callback != null) {
                    callback.onLoadSuccess(resource);
                }
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
                if (callback != null) {
                    callback.onLoadFailed();
                }
            }
        });
    }

    public static void clear(Context context, ImageView imgView) {
        Glide.with(context).clear(imgView);
    }

    public static void loadBlur(Context context, Object img, ImageView imageView) {
        if (context == null) {
            return;
        }
        Glide.with(context).asDrawable().load(img)
                .skipMemoryCache(SKIP_MEMORY_CACHE)
                .apply(RequestOptions.bitmapTransform(sBlurTransformation))
                .into(imageView);
    }

    public interface DrawableCallback {
        void onLoadSuccess(Drawable drawable);

        void onLoadFailed();

    }
}
